package com.lw.yygh.cmn.controller;

import com.lw.yygh.cmn.service.DictService;
import com.lw.yygh.model.cmn.Dict;
import com.lw.yygh.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;
    //根据数据id查询子数据列表
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> dicts=dictService.findChildData(id);
        return Result.ok(dicts);
    }
    //导出数据字典
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }
    //导入数据字典
    @PostMapping("/importData")
    public Result importDict(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }
    //根据dictcode和value查询
    @GetMapping(value = "/getName/{dictCode}/{value}",produces = "application/json;charset=utf-8")
    public String getName(@PathVariable String dictCode,@PathVariable String value) throws UnsupportedEncodingException {
        String dictName=dictService.getDictName( dictCode,value );
        return dictName;
    }
    //根据value查询
    @GetMapping(value = "/getName/{value}",produces = "application/json;charset=utf-8")
    public String getName(@PathVariable String value) throws UnsupportedEncodingException {
        String dictName=dictService.getDictName("",value);
        return dictName;
    }
    //根据dictCode获取下级节点
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> list =dictService.findByDictCode(dictCode);
        return Result.ok( list );
    }
}
